package com.egg.ylt.presenter.impl;

import com.egg.ylt.Utils.API;
import com.egg.ylt.pojo.UpLoadFileEntity;
import com.egg.ylt.pojo.UserByidEntity;
import com.egg.ylt.presenter.ICenterPresenter;
import com.egg.ylt.view.ICenterView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.io.File;
import java.util.HashMap;

/**
 * Created by sichaoqi on 2018/9/25.
 */

public class CenterPresenterImpl extends BasePresenter<ICenterView> implements ICenterPresenter {
    @Override
    public void getUserInfo(String token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        RequestManager.getInstance().requestPostByAsyn(API.GET_USER_BY_ID, hashMap, new ReqCallBack<UserByidEntity>() {

            @Override
            public void onReqSuccess(UserByidEntity result) {
                mView.getUserInfo(result);
            }

            @Override
            public void onFailure(String result) {
                mView.showException(result);
//                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showException(error.getMsg());
//                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    @Override
    public void postPhoto(String token, String name, String headImage) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("name", name);
        hashMap.put("headImage", headImage);
        RequestManager.getInstance().requestPostByAsyn(API.POST_UPDATE_USER_PHOTO, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.onPostPhoto();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    @Override
    public void upImg(String file) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, Object> hashMap = new HashMap<>();
        File photoFile = new File(file);
        hashMap.put("file", photoFile);
        RequestManager.getInstance().upLoadFile(API.UP_LOAD_FILE, hashMap, new ReqCallBack<UpLoadFileEntity>() {

            @Override
            public void onReqSuccess(UpLoadFileEntity result) {
                mView.dismissDialogLoading();
                mView.getImgUri(result);
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }
}
